package org.example.aienglishapp.service.loginlog;

import org.example.aienglishapp.entity.loginlog.LoginLog;
import org.example.aienglishapp.mapper.loginlog.LoginLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class LoginLogService {

    private static final Logger logger = LoggerFactory.getLogger(LoginLogService.class);

    @Autowired
    private LoginLogMapper loginLogMapper;

    @Transactional
    public void saveLoginLog(LoginLog loginLog) {
        try {
            loginLogMapper.insertLoginLog(loginLog);
            logger.info("登录日志已保存: {}", loginLog);
        } catch (Exception e) {
            logger.error("保存登录日志时发生错误: {}", e.getMessage(), e);
        }
    }

    @Transactional(readOnly = true)
    public List<LoginLog> getAllLoginLogs() {
        try {
            List<LoginLog> logs = loginLogMapper.selectAllLoginLogs();
            logger.info("获取到 {} 条登录日志", logs.size());
            return logs;
        } catch (Exception e) {
            logger.error("获取登录日志时发生错误: {}", e.getMessage(), e);
            return Collections.emptyList();
        }
    }

    public void deleteLoginLog(Long info_id) {
        try {
            loginLogMapper.deleteLoginLog(info_id);
            logger.info("删除登录日志: {}", info_id);
        } catch (Exception e) {
            logger.error("删除登录日志时发生错误: {}", e.getMessage(), e);
        }
    }
}
